import { request } from '@/utils/request';

export const sendVerificationCode = async (email: string): Promise<string> => {
  try {
    const response = await request.get({
      url: '/register/sendVerificationCode',
      params: { email }, // 只传递 email 参数
    });

    // 检查响应是否为字符串
    if (typeof response === 'string') {
      return response;
    }

    // 检查响应是否为对象
    if (response && typeof response === 'object') {
      if (response.code === 200) {
        return response.message || response.data;
      } else {
        throw new Error(response.message || '服务器返回了非200状态码');
      }
    }

    // 如果响应格式不符合预期，抛出错误
    throw new Error('未知的响应格式');
  } catch (error) {
    console.error('发送验证码失败:', error);
    throw error;
  }
};

// QQ发送验证码注册
export const register = async (
  email: string,
  password: string,
  verificationCode: string
): Promise<string> => {
  try {
    const response = await request.get({
      url: '/register/qqregister',
      params: { email, password, verificationCode }, // 作为 URL 查询参数传递
    });

    // 打印服务器返回的所有内容
    console.log('服务器返回的完整响应:', response);

    // 检查响应是否为字符串
    if (typeof response === 'string') {
      return response;
    }

    throw new Error('未知的响应格式');
  } catch (error) {
    console.error('注册失败:', error);
    throw error;
  }
};